﻿using Microsoft.AspNetCore.Mvc;
using car.Models;
using System.Collections.Generic;
using System.Linq;
using System;

namespace car.Controllers
{
    public class AdminController : Controller
    {
        public IActionResult Index(string q=null)
        {

            var admin = new List<Admin>();
            try
            {
                using (var db = new carContext())
                {
                    if (q != null)
                    {
                        var flag=from m in db.Admin select m;
                        flag = flag.Where( s => s.Aid.Contains(q) );
                        return View(flag);
                    }
                    else
                    {


                        admin = db.Admin.ToList();
                        ViewBag.Admin = true;
                        return View(admin);
                    }
                }
            }
            catch
            {
                ViewBag.Admin = false;
            }
            return View(null);



            
        }

        public IActionResult Create()
        {
            return View();
        }
        public IActionResult Created(Admin admin)
        {
            string id = Request.Form["id"];
            string pwd = Request.Form["pwd"];
            
            try
            {
                using (var db = new carContext())
                {
                    admin=db.Admin.Find(id);
                    if (admin != null)
                    {
                        TempData["Create"] = "Aid was been created!";
                    }
                    else
                    {
                        admin=new Admin();
                        admin.Aid = id;
                        admin.Apwd = pwd;
                        db.Admin.Add(admin);
                        TempData["Create"] = "Create success";
                    }
                    db.SaveChanges();
                }
            }
            catch(KeyNotFoundException e)
            {
                 throw e;
            }
            
            return View("Create");
        }

        public IActionResult Delete(string id)
        {
            using (var db = new carContext())
            {
                var admin=db.Admin.Find(id);
                if(admin != null)
                {
                    db.Admin.Remove(admin);
                }
                else
                {
                    ViewBag.admin = false;
                }
                db.SaveChanges();
            }
            return View("Index");
        }

        public IActionResult Details()
        {
            var admins = new List<Admin>();
            using (var db = new carContext())
            {
                admins =db.Admin.ToList();
            }
            return View(admins);
        }


        public IActionResult Update(string id)
        {
            ViewData.Clear();
            ViewData["Aid"] = id;
            return View();
        }
        public IActionResult Updated()
        {
            Admin admin;
            string id = Request.Form["aid"];
;
            string pwd = Request.Form["apwd"];
            try
            {
                using (var db = new carContext())
                {
                   admin =db.Admin.Find(id);
                    if (admin == null)
                    {
                        admin = new Admin();
                        admin.Aid = id;
                    }
                    admin.Apwd=pwd;
                    db.Admin.Update(admin);
                    db.SaveChanges(true);
                    ViewData["Flag"] = "true";
                }

            }
            catch (Exception ex)
            {
                throw ex;
                ViewData["Flag"] = "false";
            }
            return View("Update");
        }

    }
}
